//
//  WaterUtils.m
//  DCNativeApiPlugin
//
//  Created by iOSTean on 2023/5/10.
//

#import "WaterUtils.h"
#define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
@implementation WaterUtils
//从十六进制字符串获取颜色，
//color:支持@“#123456”、 @“0X123456”、 @“123456”三种格式
+ (UIColor *)sd_colorWithHexString:(NSString *)color alpha:(CGFloat)alpha{
    //删除字符串中的空格
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6){
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的，那么截取字符串，字符串从索引为2的位置开始，一直到末尾
    if ([cString hasPrefix:@"0X"]){
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的，那么截取字符串，字符串从索引为1的位置开始，一直到末尾
    if ([cString hasPrefix:@"#"]){
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6){
        return [UIColor clearColor];
    }
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha];
}
///路径转换
+ (UIImage *)converUniPath:(NSString *)unipath{
    if(kStringIsEmpty(unipath)){
        return nil;
    }
    NSMutableString *str = [NSMutableString stringWithString:unipath];
    if([unipath hasPrefix:@"_doc"]){//以_doc开头 相机
        PDRCoreAppInfo *appinfo = [PDRCore Instance].appManager.getMainAppInfo;
        [str replaceCharactersInRange:NSMakeRange(0, @"_doc".length) withString:appinfo.documentPath];
    }
    NSData *showData = [NSData dataWithContentsOfFile:str];
    UIImage *image = [UIImage imageWithData:showData];
    return image;
}
//计算文字宽度
+ (CGFloat)stringWidth:(NSString *)title withFont:(UIFont *)font{
    CGSize size = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil].size;
    return size.width;
}
//计算文字高度
+ (CGFloat)stringHeightWithString:(NSString*)string stringFont:(UIFont*)font textWidth:(CGFloat)width{
    CGRect textRect = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                        attributes:@{ NSFontAttributeName : font }
                                           context:nil];
    CGSize labelSize = textRect.size;
    return labelSize.height;
}
+(BOOL)clearCacheWithFilePath:(NSString *)path{
    if(kStringIsEmpty(path)){
        return NO;
    }
    NSMutableString *pothoStr = [NSMutableString stringWithString:path];
    if([pothoStr hasPrefix:@"_doc"]){//以_doc开头 相机
        PDRCoreAppInfo *appinfo = [PDRCore Instance].appManager.getMainAppInfo;
        [pothoStr replaceCharactersInRange:NSMakeRange(0, @"_doc".length) withString:appinfo.documentPath];
    }
    NSError *error = nil;
    BOOL b = [[NSFileManager defaultManager] removeItemAtPath:pothoStr error:&error];

    return b;
}
@end
